//
//  NSStringAddition.m
//  XPPXcodePlugin
//
//  Created by bo Ye on 10-9-19.
//  Copyright 2010 none. All rights reserved.
//

#import "NSStringAddition.h"


@implementation NSString (YeAddition)
- (BOOL)isStringContainOtherString:(NSString*)string2{
	NSRange range = [self rangeOfString:string2];
	if (NSNotFound != range.location) {
		return YES;
	}
	return NO;
}



- (NSRange)rangeOfStringBetweenStartWith:(NSString*)startString endWith:(NSString *)endString
{
	NSRange rangeStart = [self rangeOfString:startString];
//	NSRange rangeEnd = [self rangeOfString:endString];
	if (NSNotFound == rangeStart.location ) {
		return rangeStart;
	}
	NSRange rangeEnd = [self rangeOfString:endString
								   options:NSLiteralSearch
									 range:NSMakeRange(rangeStart.location + rangeStart.length, [self length] - rangeStart.location -rangeStart.length)];
	
	if (rangeStart.location != NSNotFound || rangeEnd.location != NSNotFound) {
		return NSMakeRange(rangeStart.location + rangeStart.length, rangeEnd.location - rangeStart.location - rangeStart.length);
	}
	return NSMakeRange(NSNotFound, 0);
}


- (NSRange)rangeOfStringStartWith:(NSString*)startString endWith:(NSString *)endString 
{
	NSRange rangeStart = [self rangeOfString:startString];
	if(rangeStart.location == NSNotFound) return rangeStart;
	NSRange rangeEnd = [self rangeOfString:endString
								   options:NSLiteralSearch
									 range:NSMakeRange(rangeStart.location, [self length]- rangeStart.location)];
	
	if (rangeStart.location != NSNotFound && rangeEnd.location != NSNotFound) {
		return NSMakeRange(rangeStart.location, rangeEnd.location - rangeStart.location +rangeEnd.length);
	}
	return NSMakeRange(NSNotFound, 0);
}



- (NSString *)subStringSourceBetweenStringStart:(NSString *)startString end:(NSString *)endString
{
	NSRange subRange = [self rangeOfStringBetweenStartWith:startString endWith:endString];
	if (subRange.location != NSNotFound) {
		return [self substringWithRange:subRange];
	}
	return nil;
}

- (NSString *)subStringSourceStringStart:(NSString *)startString end:(NSString *)endString
{
	NSRange subRange = [self rangeOfStringStartWith:startString endWith:endString ];
	if (subRange.location != NSNotFound) {
		return [self substringWithRange:subRange];
	}
	return nil;
}



- (NSString *)getRidOfStringStartWith:(NSString*)startString endWith:(NSString *)endString
{
	NSString *opreateString = [[self copy] autorelease];
	do {
		NSRange subRange = [opreateString rangeOfStringStartWith:startString endWith:endString];
		if (subRange.location != NSNotFound) {
			opreateString = [opreateString stringByReplacingCharactersInRange:subRange withString:@""];
		}else {
			return opreateString;
		}
		
	} while (YES);
	
	return opreateString;
}

//从字符串中取出 所有 以startString 开头 endString 字符串 以数组返回 包括 startString

- (NSString*)getTheStringBetweenStartWith:(NSString *)startString endWith:(NSString *)endString{
	NSString *sourceString = [[self copy] autorelease];
	NSMutableArray * resultArray = [NSMutableArray array];
	do {
		NSString *line = [sourceString subStringSourceBetweenStringStart:startString end:endString];
		if (line  == nil) {
			break;
		}
		[resultArray addObject:line];
		NSRange nextRange = [sourceString rangeOfString:endString];
		if (nextRange.location == NSNotFound) {
			break;
		}
		sourceString = [sourceString substringFromIndex:nextRange.location + nextRange.length];
	} while (YES);
	
	return [resultArray count] > 0 ? resultArray : nil;
}
//从字符串中取出 所有 以startString 开头 endString 字符串 以数组返回 不包括 startString

- (NSArray *)getAllTheStringBetweenStartWith:(NSString *)startString endWith:(NSString *)endString
{
	NSString *sourceString = [[self copy] autorelease];
	NSMutableArray * resultArray = [NSMutableArray array];
	do {
		NSString *line = [sourceString subStringSourceBetweenStringStart:startString end:endString];
		if (line  == nil) {
			break;
		}
		[resultArray addObject:line];
		NSRange nextRange = [sourceString rangeOfString:endString];
		if (nextRange.location == NSNotFound) {
			break;
		}
		sourceString = [sourceString substringFromIndex:nextRange.location + nextRange.length];
	} while (YES);

	return [resultArray count] > 0 ? resultArray : nil;
}


- (NSString*)removeAllComments
{
	NSString *string = [[self copy] autorelease];
	string = [string getRidOfStringStartWith:@"//" endWith:@"\n"];
	string = [string getRidOfStringStartWith:@"/*" endWith:@"*/"];
	return string;
}

//判断 指定范围内的字符串是否在注释中

- (BOOL)isStringAtRangeInComments:(NSRange)range
{
	//remove the comments in string before the range
	NSRange rangeQ = NSMakeRange(0, range.location);
	NSString *stringBefore = [[self substringWithRange:rangeQ] removeAllComments];
	NSRange rangeC = [stringBefore rangeOfString:@"/*"];
	if (rangeC.location != NSNotFound) {
		return YES;
	}
	NSRange rangeD = [stringBefore rangeOfString:@"//"];
	if (rangeD.location != NSNotFound) {
		return YES;
	}
	return NO;
}


@end
